package com.taobao.order.controller;

import com.taobao.basic.util.JsonResult;
import com.taobao.basic.util.LoginContext;
import com.taobao.basic.util.PageList;
import com.taobao.order.domain.OrderPetAcquisition;
import com.taobao.order.dto.OrderDto;
import com.taobao.order.query.OrderPetAcquisitionQuery;
import com.taobao.order.service.IOrderPetAcquisitionService;
import com.taobao.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

// @Controller
// @ResponseBody
@RestController // controller层，返回json格式数据
@RequestMapping("/orderPetAcquisition")
@Api(value = "宠物的API",description="宠物相关的CRUD功能")
public class OrderPetAcquisitionController {

    @Autowired
    private IOrderPetAcquisitionService orderPetAcquisitionService;


    /**
     * 分页数据
     *
     * @return
     */
    @ApiOperation(value = "分页查询接口")
    @PostMapping
    public PageList<OrderPetAcquisition> queryPage(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request) {
        try {

            Logininfo logininfo = LoginContext.getLogininfo(request);

            return orderPetAcquisitionService.pageQuery(query, logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<OrderPetAcquisition>();
        }
    }

    /**
     * 确认订单
     *
     * @return
     */
    @ApiOperation(value = "确认订单接口")
    @PostMapping("/confirm")
    public JsonResult confirm(@RequestBody OrderDto dto) {
        try {
            // 哈哈哈
            orderPetAcquisitionService.confirm(dto);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("订单确认失败！");
        }
    }

    /**
     * 取消订单
     *
     * @return
     */
    @ApiOperation(value = "取消订单接口")
    @DeleteMapping("/{id}")
    public JsonResult cancel(@PathVariable("id") Long id) {
        try {
            // 哈哈哈
            orderPetAcquisitionService.cancel(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("订单取消失败！");
        }
    }
}